<?php

/**
 * @copyright 2012
 */

require_once("SqlUtils.php");
require_once("Account.php");
require_once("Turnover.php");

//不能变
$type = $_GET["type"];
$id = $_GET["id"];

$remark = $_GET["remark"];
$time = $_GET["time"];
$catid = $_GET["catid"];

$money = $_GET["money"];
$from = $_GET["from"];
$to = $_GET["to"];


$turnover = new Turnover();
$turnover->Init($id);

if($remark)
{
    $turnover->remark = $remark;
}
if($catid)
{
    $turnover->catid = $catid;
}
if($time)
{
    $turnover->time = $time;
}

$modifiedAccountArray = array();
if($turnover->type == 1)//收入
{
    if($to && $to != $turnover->to)
    {
        $old_acc = new Account();
        $old_acc->Init($turnover->to);
        $old_acc->money -= $turnover->money;
        $old_acc->Update();
        
        $new_acc = new Account();
        $new_acc->Init($to);
        $new_acc->money += $turnover->money;
        $new_acc->Update();
        
        $turnover->to = $to;
        
        array_push($modifiedAccountArray,$new_acc);
        array_push($modifiedAccountArray,$old_acc);
    }
    
    if($money)
    {
        $acc = new Account();
        $acc->Init($turnover->to);
        $acc->money -= $turnover->money;
        $acc->money += $money;
        $acc->Update();
        $turnover->money = $money;
        
        if(!$to)
        {
            array_push($modifiedAccountArray,$acc);
        }
    }
} else if($turnover->type == 0)//支出
{
    if($from && $from != $turnover->from)
    {
        $old_acc = new Account();
        $old_acc->Init($turnover->from);
        $old_acc->money += $turnover->money;
        $old_acc->Update();
        
        $new_acc = new Account();
        $new_acc->Init($from);
        $new_acc->money -= $turnover->money;
        $new_acc->Update();
        
        $turnover->from = $from;
        
        array_push($modifiedAccountArray,$new_acc);
        array_push($modifiedAccountArray,$old_acc);
    }
    
    if($money)
    {
        $acc = new Account();
        $acc->Init($turnover->from);
        $acc->money += $turnover->money;
        $acc->money -= $money;
        $acc->Update();
        
        $turnover->money = $money;
        
        if(!$from)
        {
            array_push($modifiedAccountArray,$acc);
        }
    }
} else if($turnover->type == 2)//转账
{
    if($money)
    {
        $from_acc = new Account();
        $from_acc->Init($turnover->from);
        
        $to_acc = new Account();
        $to_acc->Init($turnover->to);
        
        $from_acc->money += $turnover->money;
        $to_acc->money -= $turnover->money;
        
        $from_acc->money -= $money;
        $to_acc->money += $money;
        
        
        $from_acc->Update();
        $to_acc->Update();
        
         $turnover->money = $money;
        
        array_push($modifiedAccountArray,$from_acc);
        array_push($modifiedAccountArray,$to_acc);
    }
    
}

 $turnover->Update();
/////////////////////////
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  

  
  $r = $doc->createElement("accounts");
  $doc->appendChild($r);
  
 // print_r($modifiedAccountArray);
  foreach($modifiedAccountArray as $acc)
  {
       $node = $acc->GetXMLNode($doc);
       $r->appendChild($node);
  }
        
    
    $t = $doc->createElement("turnovers");
    $doc->appendChild($t);
    $t->appendChild($turnover->GetXMLNode($doc));

    echo $doc->saveXML();



?>